﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        String uname = TextBox1.Text.Trim();
        String pwd = TextBox1.Text.Trim();

        DBClass db = new DBClass();
        String sql = "";
        if (RadioButtonList1.SelectedValue == "1")//读者登录
        {
            sql = "select * from reader where rid='" + uname + "' and pwd='" + pwd + "'";
            DataTable dt1 = db.GetRecords(sql);

            if (dt1 != null && dt1.Rows.Count == 1)//登录成功
            {

                if (dt1.DefaultView[0]["isvalid"].ToString() == "1")//账户有效
                {

                    Session["reader"] = uname;
                    Response.Redirect("reader/main.aspx");

                }
                else
                {
                    Response.Write("<script languge='javascript'>alert（'你的账户已被暂停使用，请联系管理员!'）;</script>");

                }
            }
            else
            {
                Response.Write("<script languge='javascript'>alert（'无法登录，用户名或密码错误!'）;</script>");
            }
        }
        else //管理员登录
        {
            sql = "select * from admin where aid='" + uname + "' and pwd='" + pwd + "'";
            DataTable dt2 = db.GetRecords (sql);

            if (dt2 != null && dt2.Rows.Count == 1)//登录成功
            {
                Session["admin"] = uname;
                Response.Redirect("admin/main.aspx");
            }
            else {
                Response.Write("<script languge='javascript'>alert（'无法登录，用户名或密码错误!'）;</script>");
            }
        }

    }
}